#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
using ull = unsigned long long;
const int N = 5e5 + 5;

int n, m;
char s[N], t[N];
ull f[N], g[N], p[N];
bool check(int i, int len) {
  ull left = f[i] - f[i - len] * p[len];
  ull right = g[i + 1] - g[i + len + 1] * p[len];
  return left == right;
}
int search(int i) {
  int l = 0, h = min(i, n - i);
  while (l <= h) {
    int mid = (l + h) >> 1;
    if (check(i, mid))
      l = mid + 1;
    else
      h = mid - 1;
  }
  return h;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> s[i];
  p[0] = 1;
  rep(i, 1, n) p[i] = p[i - 1] * 131;
  rep(i, 1, n) f[i] = f[i - 1] * 131 + s[i] - '0';
  per(i, n, 1) g[i] = g[i + 1] * 131 + ((s[i] - '0') ^ 1);
  ll ans = 0;
  rep(i, 1, n - 1) {
    int len = search(i);
    // cout << i << ' ' << len << endl;
    ans += len;
  }
  cout << ans;
  return 0;
}